city = [{'code': '410', 'name': '全国'},
      {'code': '010', 'name': '北京'},
      {'code': '020', 'name': '上海'},
      {'code': '030', 'name': '天津'},
      {'code': '040', 'name': '重庆'},
      {'code': '050020', 'name': '广州'},
      {'code': '050090', 'name': '深圳'},
      {'code': '060080', 'name': '苏州'},
      {'code': '060020', 'name': '南京'},
      {'code': '070020', 'name': '杭州'},
      {'code': '210040', 'name': '大连'},
      {'code': '280020', 'name': '成都'},
      {'code': '170020', 'name': '武汉'},
      {'code': '050060', 'name': '惠州'},
      {'code': '050050', 'name': '佛山'},
      {'code': '050130', 'name': '中山'},
      {'code': '050040', 'name': '东莞'},
      {'code': '050140', 'name': '珠海'},
      {'code': '180020', 'name': '长沙'},
      {'code': '150020', 'name': '郑州'},
      {'code': '210020', 'name': '沈阳'},
      {'code': '170020', 'name': '武汉'},
      {'code': '250070', 'name': '青岛'},
      {'code': '080020', 'name': '合肥'},
      {'code': '070030', 'name': '宁波'},
      {'code': '310020', 'name': '昆明'},
      {'code': '270020', 'name': '西安'}]

workExperiences = [{'code': '1', 'name': '应届生'},
             {'code': '2', 'name': '实习生'},
             {'code': '0$1', 'name': '1年以内'},
             {'code': '1$3', 'name': '1-3年'},
             {'code': '3$5', 'name': '3-5年'},
             {'code': '5$10', 'name': '5-10年'},
             {'code': '10$999', 'name': '10年以上'}]

compScales = [{"code":"010","name":"1-49人"},
          {"code":"020","name":"50-99人"},
          {"code":"030","name":"100-499人"},
          {"code":"040","name":"500-999人"},
          {"code":"050","name":"1000-2000人"},
          {"code":"060","name":"2000-5000人"},
          {"code":"070","name":"5000-10000人"},
          {"code":"080","name":"10000人以上"}]

# header准备
def headers():
    liepin_headers = {
        "Accept": "application/json, text/plain, */*",
        "Accept-Encoding": "gzip, deflate, br",
        "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-US;q=0.7,en-CA;q=0.6",
        "Connection": "keep-alive",
        "Content-Length": "482",
        "Content-Type": "application/json;charset=UTF-8;",
        "Cookie": "inited_user=464b5299797bde79a6aeac4e54e72749; __gc_id=fcb411cd19404d6c8519f9289c795f0a; _ga=GA1.1.737936061.1681902650; __uuid=1681902669648.13; need_bind_tel=false; c_flag=92fc82dab3c60b517e05029c26bb865c; imClientId=b5e1273d5cb093f4b435a5bdfa4014a6; imId=b5e1273d5cb093f407ef590fee2a579a; imClientId_0=b5e1273d5cb093f4b435a5bdfa4014a6; imId_0=b5e1273d5cb093f407ef590fee2a579a; new_user=false; access_system=C; XSRF-TOKEN=WR6vUQM2QK-QgJ1xoWMumA; __tlog=1703127510250.65%7C00000000%7C00000000%7C00000000%7C00000000; acw_tc=276077c317031275113723694e8f031181610e8406eab2e035f042a0d1a998; Hm_lvt_a2647413544f5a04f00da7eee0d5e200=1701342916,1701422246,1703076291,1703127512; UniqueKey=cb5430ca53f7a1c16bc56763ae71c6f2; liepin_login_valid=0; lt_auth=vuhcM3ECnA7%2F5nba2zFbta1O2o%2BsAzrM8S4LgUgC1YW6XqXg4P%2FmQAOHqrEG%2BCoIq08jdvkzMLf%2BNun8znZC6EUQ8VGnlZ6utf6k0HsDUeVgI8W2vezHg%2FXUQp0lnEAA8nJbpEIL%2BVzO; user_roles=0; user_photo=5f8fa3baf6d1ab58476f324908u.png; user_name=%E5%90%B4%E8%8C%B5%E8%8C%B5; inited_user=464b5299797bde79a6aeac4e54e72749; hpo_role-sec_project=sec_project_liepin; hpo_sec_tenant=0; imApp_0=1; Hm_lpvt_a2647413544f5a04f00da7eee0d5e200=1703127593; fe_im_socketSequence_new_0=2_2_2; __session_seq=20; __uv_seq=20; __tlg_event_seq=571; fe_im_opened_pages=; fe_im_connectJson_0=%7B%220_cb5430ca53f7a1c16bc56763ae71c6f2%22%3A%7B%22socketConnect%22%3A%222%22%2C%22connectDomain%22%3A%22liepin.com%22%7D%7D; _ga_54YTJKWN86=GS1.1.1703127511.24.1.1703128842.0.0.0",
        "Host": "api-c.liepin.com",
        "Origin": "https://www.liepin.com",
        "Referer": "https://www.liepin.com/",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-site",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
        "X-Client-Type": "web",
        "X-Fscp-Bi-Stat": "{\"location\": \"https://www.liepin.com/zhaopin/?currentPage=0&pageSize=40&city=050090&dq=050090&pubTime=&key=%E4%BA%A7%E5%93%81%E7%BB%8F%E7%90%86&suggestTag=&otherCity=&industry=&ckId=geq5b0qsi9umzbhn0oojbi2j8v8qu2bb&scene=condition&skId=geq5b0qsi9umzbhn0oojbi2j8v8qu2bb&fkId=geq5b0qsi9umzbhn0oojbi2j8v8qu2bb&sfrom=search_job_pc&suggestId=\"}",
        "X-Fscp-Fe-Version": "",
        "X-Fscp-Std-Info": "{\"client_id\": \"40108\"}",
        "X-Fscp-Trace-Id": "af6eb714-1bff-40c7-8c8c-20bf44814785",
        "X-Fscp-Version": "1.1",
        "X-Requested-With": "XMLHttpRequest",
        "X-XSRF-TOKEN": "WR6vUQM2QK-QgJ1xoWMumA"
    }

    return liepin_headers


# 准备城市code
def choose_city(city_name):
    for i in city:
        if i['name'] == city_name:
            return i['code']

# 准备工作经验code
def choose_WE(工作经验):
    for i in workExperiences:
        if i['name'] == 工作经验:
            return i['code']
        
# 准备公司规模code
def choose_WS(公司规模):
    for i in compScales:
        if i['name'] == 公司规模:
            return i['code']
        
# 准备payload
def request_payload(城市,关键词,工作经验,公司规模,页码="0"):
    payload = {
        "data": {
            "mainSearchPcConditionForm": {
                "city": "050020",
                "dq": choose_city(城市),
                "pubTime": "",
                "currentPage": 页码,
                "pageSize": 40,
                "key": "文案",
                "suggestTag": "",
                "workYearCode": choose_WE(工作经验),
                "compId": "",
                "compName": "",
                "compTag": "",
                "industry": "",
                "salary": "",
                "jobKind": "",
                "compScale": choose_WS(公司规模),
                "compKind": "",
                "compStage": "",
                "eduLevel": ""
            },
            "passThroughForm": {
                "scene": "input",
                "skId": "y7sk8vljpy6qkk10c4lbnmvks8cxa589",
                "fkId": "nsuom13dhuo5fohu9kfblhp3z7yqwx0a",
                "ckId": "bbj6whpz3502xykws0ti6rlvtwqzfzzb",
                "suggest": None
            }
        }
    }
    return payload
    